package com.daka.service.impl;

import com.daka.dao.VolunteerDao;
import com.daka.dao.VolunteerWelfareDao;
import com.daka.dao1.WelfareOrderDao;
import com.daka.entity.Volunteer;
import com.daka.entity.VolunteerWelfare;
import com.daka.entity.WelfareOrder;
import com.daka.service.VolunteerWelfareService;
import com.daka.tools.ConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Slf4j
@Service("volunteerWelfareService")
public class VolunteerWelfareServiceImpl implements VolunteerWelfareService {

    @Resource
    private VolunteerWelfareDao volunteerWelfareDao;

    @Resource
    private WelfareOrderDao welfareOrderDao;

    @Resource
    private VolunteerDao volunteerDao;

    public void migrate(Integer max, Integer min) {

        List<WelfareOrder> welfareOrderList;
        if (max == 0 || min == 0) {
            welfareOrderList = welfareOrderDao.selectAll();
        } else {
            welfareOrderList = welfareOrderDao.selectByIdBetween(max, min);
        }

        List<VolunteerWelfare> volunteerWelfareList = new ArrayList<VolunteerWelfare>();

        for (WelfareOrder welfareOrder : welfareOrderList) {

            log.info("===========> member" + welfareOrder.getMember());

            Volunteer volunteer = volunteerDao.selectByOldId(welfareOrder.getMember());

            if (volunteer != null) {

                volunteerWelfareList.add(VolunteerWelfare.build(welfareOrder, volunteer.getMemberCode()));

                if (volunteerWelfareList.size() >= ConfigUtil.BATCH_INSERT_MAX) {
                    int i = volunteerWelfareDao.insertBatch(volunteerWelfareList);

                    if (i > 0) {
                        volunteerWelfareList.clear();
                    }
                }

            }


        }

        if (volunteerWelfareList.size() > 0) {
            volunteerWelfareDao.insertBatch(volunteerWelfareList);
        }
    }
}
